package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.StoreDao;
import com.delivery.www.po.Store;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StoreDaoImpl implements StoreDao {
    @Override
    public int createStore(String name, String introduce, int id, int dishNum, int commentNum, int proOrderNum, int unproOrderNum) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Object[] params = {name, introduce, id, dishNum, commentNum, proOrderNum, unproOrderNum};
        String sql = "insert into store (store_name,introduce,seller_id,dish_num,comment_num,process_order_num,unprocess_order_num) values(?,?,?,?,?,?,?)";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        if (excute > 0) {
            Object[] params1 = {id};
            String sql1 = "select store.id from store where seller_id = ?";
            rs = JdbcUtils.excute(connection, pstm, rs, sql1, params1);
            int i = 0;
            try {
                if (rs.next()) {
                    i = rs.getInt("store_id");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            Object[] params2 = {i, id};
            String sql2 = "update seller set store_id = i where seller_id = ?";
            JdbcUtils.excute(connection, pstm, sql2, params2);
        }
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }

    @Override
    public Store getOwnStore(int id) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Object[] params = {id};
        String sql = "select s.store_id,s.store_name,s.introduce,s.dish_num,s.comment_num,s.process_order_num,s.unprocess_order_num from store s where seller_id = ?";
        rs = JdbcUtils.excute(connection, pstm, rs, sql, params);
        Store store = null;
        try {
            if (rs.next()) {
                store = new Store();
                store.setStoreId(rs.getInt("store_id"));
                store.setStoreName(rs.getString("store_name"));
                store.setIntroduce(rs.getString("introduce"));
                store.setDishNum(rs.getInt("dish_num"));
                store.setCommentNum(rs.getInt("comment_num"));
                store.setProcessOrderNum(rs.getInt("process_order_num"));
                store.setUnprocessOrderNum(rs.getInt("unprocess_order_num"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.closeResource(connection, pstm, rs);
        return store;
    }

    @Override
    public List<Store> getAllStore() {
        Connection conn = null;
        conn = JdbcUtils.getConnection();
        List<Store> stores = new ArrayList<>();
        if (conn != null) {
            PreparedStatement pstm = null;
            ResultSet rs = null;
            Object[] params = {};
            String sql = "select s.store_id,s.store_name,s.introduce,s.dish_num,s.comment_num from store s";
            rs = JdbcUtils.excute(conn, pstm, rs, sql, params);
            try {
                while (rs.next()) {
                    Store store = new Store();
                    store.setStoreId(rs.getInt("store_id"));
                    store.setStoreName(rs.getString("store_name"));
                    store.setIntroduce(rs.getString("introduce"));
                    store.setDishNum(rs.getInt("dish_num"));
                    store.setCommentNum(rs.getInt("comment_num"));
                    stores.add(store);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            JdbcUtils.closeResource(conn, pstm, rs);
        }
        return stores;
    }

    @Override
    public int updateStoreName(int storeId, String value) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {value,storeId};
        String sql = "update store set store_name = ? where store_id = ?";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }

    @Override
    public int updateStoreIntroduce(int storeId, String value) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {value, storeId};
        String sql = "update store set introduce = ? where store_id = ?";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }
}
